#include "gohomeform.h"
#include "ui_gohomeform.h"
#include<QComboBox>
#include<QTimer>
#include"data.h"
#include"liwishell.h"


GoHomeForm::GoHomeForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::GoHomeForm)
{
    ui->setupUi(this);

    connect(ui->pushButton_start,&QPushButton::clicked,this,[=](){on_Button_start_clicked();});
    connect(ui->pushButton_stop,&QPushButton::clicked,this,[=](){on_PushButton_stop_clicked();});
    connect(ui->pushButton_cleanpos,&QPushButton::clicked,this,[=](){on_PushButton_cleanpos_clicked();});


    QTimer *timer1=new QTimer(this);
    connect(timer1,&QTimer::timeout,this,&GoHomeForm::updateWin);
    timer1->start(100);
}

GoHomeForm::~GoHomeForm()
{
    delete ui;
}

void GoHomeForm:: on_Button_start_clicked(){
    if(Data::handle!=nullptr){

        // bool chkopen[4];
        // int fwdIo[4];
        // int revIo[4];
        // int homeIo[4];
        // float gobackCT[4];
        // float pyDis[4];
        // float creepSp[4];
        ui->pushButton_start->setEnabled(false);
        ui->label_flag->setText("运行");
        ui->label_flag->setStyleSheet("background-color: rgb(0,0,255)");

        PMC_Base(Data::handle,4,axislist);
        for(int i=0;i<4;++i){
            PMC_SetAxisEnable(Data::handle,axislist[i],1);
            PMC_SetAtype(Data::handle,axislist[i],1);
            PMC_SetUnits(Data::handle,axislist[i],200);
            PMC_SetSpeed(Data::handle,axislist[i],1000);
            PMC_SetAccel(Data::handle,axislist[i],5000);
            PMC_SetDecel(Data::handle,axislist[i],5000);
        }

        bool ok;
        int model=ui->checkBox_goback->isChecked()?(ui->Box_gomode->currentText()).toInt(&ok,10)+10:(ui->Box_gomode->currentText()).toInt(&ok,10);
        for(int i=1;i<=4;++i){
            // 回原开关
            QString checkOpen=QString("checkBox_open%1").arg(i);
            QCheckBox *_chkopen=findChild<QCheckBox *>(checkOpen);
            bool chkopen=_chkopen->isChecked();
            int enable=0;
            PMC_GetAxisEnable(Data::handle,i,&enable);
            if(enable==0){
                QMessageBox::about(NULL,"Warn",QString("AXIS%1未使能，请先使能！！！").arg(i));
                return;
            }

            if(chkopen){
                //正限位
                QString combofwd=QString("comboBox_%1").arg(i);
                QComboBox *_comfwd=findChild<QComboBox*>(combofwd);
                int comfwd=(_comfwd->currentText()).toInt(&ok,10);
                //负限位
                QString comborev=QString("comboBox_%1").arg(i+4);
                QComboBox *_comrev=findChild<QComboBox*>(comborev);
                int comrev=(_comrev->currentText()).toInt(&ok,10);
                //原点
                QString combohome=QString("comboBox_%1").arg(i+8);
                QComboBox *_comhome=findChild<QComboBox*>(combohome);
                int comhome=(_comhome->currentText()).toInt(&ok,10);
                //反找等待CT
                QString spinboxwait=QString("spinBox_wait%1").arg(i);
                QSpinBox *_spinwait=findChild<QSpinBox*>(spinboxwait);
                int spinwait=_spinwait->value();
                //偏移距离
                QString spinboxpy=QString("spinBox_py%1").arg(i);
                QSpinBox *_spinpy=findChild<QSpinBox*>(spinboxpy);
                int spinpy=_spinpy->value();
                //爬行速度
                QString spinboxcreep=QString("spinBox_creep%1").arg(i);
                QSpinBox *_spincreep=findChild<QSpinBox*>(spinboxcreep);
                int spincreep=_spincreep->value();

                PMC_SetFwdIn(Data::handle,i-1,comfwd);
                PMC_SetRevIn(Data::handle,i-1,comrev);
                PMC_SetHomeWait(Data::handle,i-1,spinwait);
                //PMC_BusCmd_SetDatumOffpos(Data::handle,i-1,spinpy);
                PMC_SetCreep(Data::handle,i-1,spincreep);
                PMC_SetDatumIn(Data::handle,i-1,comhome);
                PMC_Single_Datum(Data::handle,i-1,model);

            }

        }

    }



}
//停止
void GoHomeForm::on_PushButton_stop_clicked(){
    if(Data::handle!=nullptr){
        PMC_Rapidstop(Data::handle,3);
        ui->pushButton_start->setEnabled(true);
        runflage=false;
        ui->label_flag->setText("停止");
        ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
    }

}

//坐标清零
void GoHomeForm::on_PushButton_cleanpos_clicked(){
    if(Data::handle!=nullptr){
        for(int i=0;i<4;++i){
            PMC_SetDpos(Data::handle,axislist[i],0);
        }
    }
}

//窗口更新
void GoHomeForm::updateWin(){

    if(Data::handle!=nullptr){
        float dpos=0;
        for(int i=0;i<4;++i){
            PMC_GetDpos(Data::handle,axislist[i],&dpos);
            QString pos = QString::number( dpos,'f',3);
            if(i==0) ui->label_xpos->setText(pos);
            else if(i==1) ui->label_ypos->setText(pos);
            else if(i==2) ui->label_zpos->setText(pos);
            else if(i==3) ui->label_upos->setText(pos);
        }
        int value[4]={0,0,0,0};
        for(int i=0;i<4;++i){
            PMC_GetIfIdle(Data::handle,i,&value[i]);
        }
            //cout<<value[0]<<"_"<<value[1]<<"_"<<value[2]<<"_"<<value[3]<<endl;
        if(value[0]==-1&&value[1]==-1&&value[2]==-1&&value[3]==-1){
            runflage=false;
            ui->pushButton_start->setEnabled(true);
            ui->label_flag->setText("停止");
            ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
        }

    }


}


void GoHomeForm::showEvent(QShowEvent *event){
    QList<QString> optionsIO;
    QList<QString> optionsmode={"1","2","3","4","5","6","8","9","21"};
    for(int j=0;j<40;++j){
        optionsIO.append(QString::number(j+1));
    }

    for(int i=1;i<=12;++i){
        // 获取所有名为 "box_*" 的 ComboBox 控件
        QString comboBox=QString("comboBox_%1").arg(i);
        QComboBox *comboBoxs=findChild<QComboBox *>(comboBox);
        comboBoxs->addItems(optionsIO);
    }
    ui-> Box_gomode->addItems(optionsmode);

}

void GoHomeForm::showWarn(QString message){
     warn=QMessageBox::information(this,tr("warn"),message);
}
